package com.aliya.glide

import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.aliya.glide.databinding.ActivityLifecycleBinding
import com.aliya.glide.ktx.viewBindings
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.DrawableImageViewTarget
import com.bumptech.glide.request.transition.Transition


/**
 * 演示Glide生命周期管理
 */
class LifecyclesActivity : AppCompatActivity() {
    companion object {
        const val TAG = "LifecyclesActivity"
    }

    private val viewBindings by viewBindings(ActivityLifecycleBinding::bind)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_lifecycle)
        var url: String? = "https://up.enterdesk.com/edpic_source/53/0a/da/530adad966630fce548cd408237ff200.jpg"
        viewBindings.loadByView.setOnClickListener {
            viewBindings.ivImage.run {
                Glide.with(this)
                    .load(url)
                    .placeholder(R.drawable.image_placeholder)
                    .error(R.drawable.image_error)
                    .into(object : DrawableImageViewTarget(this) {

                        override fun onLoadCleared(placeholder: Drawable?) {
                            super.onLoadCleared(placeholder)
                            Log.e(TAG, "Target#onLoadCleared: $placeholder")
                        }

                        override fun onLoadFailed(errorDrawable: Drawable?) {
                            super.onLoadFailed(errorDrawable)
                            Log.e(TAG, "Target#onLoadFailed: $errorDrawable")
                        }

                        override fun onLoadStarted(placeholder: Drawable?) {
                            super.onLoadStarted(placeholder)
                            Log.e(TAG, "Target#onLoadStarted: $placeholder")
                        }

                        override fun onResourceReady(
                            resource: Drawable,
                            transition: Transition<in Drawable>?
                        ) {
                            super.onResourceReady(resource, transition)
                            Log.e(TAG, "Target#onResourceReady: $resource")
                        }

                        override fun onStart() {
                            super.onStart()
                            Log.e(TAG, "BaseTarget#onStart: ")
                        }

                        override fun onStop() {
                            super.onStop()
                            Log.e(TAG, "BaseTarget#onStop: ")
                        }

                        override fun onDestroy() {
                            super.onDestroy()
                            Log.e(TAG, "BaseTarget#onDestroy: ")
                        }

                    })

            }
        }

        viewBindings.loadPause.setOnClickListener {
            viewBindings.ivImage.run {
//                Glide.with(this).pauseRequests() // 取消 Glide.with() 绑定组件管理的进行中的 Request;
                Glide.with(this).pauseRequestsRecursive() // 取消 Glide.with() 绑定组件及依附子组件管理的进行中的 Request;
            }
        }

        viewBindings.loadPauseAll.setOnClickListener {
            viewBindings.ivImage.run {
                Glide.with(this).pauseAllRequests() // 取消 Glide.with() 绑定组件管理的进行中的Request，并清除已完成的资源;
            }
        }

    }

    private fun dimen(id: Int) = resources.getDimensionPixelSize(id)
}